<template>
  <div class="container">
    <div class="top">
      <el-input
        style="width: 200px; margin-bottom: 5px"
        v-model="search.keyword"
        placeholder="请输入单号"
        clearable
      ></el-input>
      <el-button
        type="primary"
        class="handleSearch"
        icon="el-icon-search"
        @click="handleSearch()"
        >查询</el-button
      >
      <el-button icon="el-icon-circle-plus" type="primary" @click="addAllot"
        >添加费用项目单</el-button
      >
    </div>
    <el-table
      max-height="600"
      :data="tableData"
      border
      style="width: 100%"
      empty-text="暂无数据"
    >
      <el-table-column
        prop="type_name"
        label="名称"
        align="center"
      ></el-table-column>
      <el-table-column
        prop="type_code"
        label="Code"
        align="center"
      ></el-table-column>
      <el-table-column
        prop="is_accountMsg"
        label="是否收账"
        align="center"
      ></el-table-column>
      <el-table-column
        prop="is_can_ableMsg"
        label="状态"
        align="center"
      ></el-table-column>
      <el-table-column
        prop="notes"
        label="备注"
        align="center"
      ></el-table-column>

      <el-table-column label="操作" width="200" align="center">
        <template slot-scope="scope">
          <el-button
            type="primary"
            size="mini"
            icon="el-icon-edit"
            v-if="scope.row.is_can_able!=2"
            @click="handleEdit(scope)"
            >编辑</el-button
          >
          <el-button
            size="mini"
            type="danger"
            icon="el-icon-delete"
            @click="handleDelete(scope)"
            >弃用</el-button
          >
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页 -->
    <pagination
      @page="handleSizeChange"
      @pagesize="handleCurrentChange"
      :pageData="pageData"
    ></pagination>
    <!-- 添加、修改Dailog -->
    <expenseItemsDialog :dialog="dialog" @upData="getData"></expenseItemsDialog>
  </div>
</template>
<script>
import Pagination from "@/components/pagination";

import ExpenseItemsDialog from "@/components/expenseDoc/expenseItemsDialog";
import { indexItem, cancelItem } from "@/api/expenseDoc/expenseItems";
export default {
  components: {
    pagination: Pagination,
    expenseItemsDialog: ExpenseItemsDialog
  },
  data() {
    return {
      //页面数据
      tableData: [],
      //分页
      pageData: {
        pageSizes: [10, 20],
        pageSize: 10,
        page: 1,
        total: null,
        layout: "total, sizes, prev, pager, next, jumper"
      },

      search: {
        keyword: ""
      },

      //dialog信息
      dialog: {
        dialogVisible: false,
        formData: {},
        id: ""
      }
    };
  },

  created() {
    //页面数据
    this.getData();
  },
  methods: {
    //页面数据
    getData() {
      let data = {
        limit: this.pageData.pageSize,
        page: this.pageData.page,
        keyword: this.search.keyword
      };
      indexItem(data)
        .then(res => {
          if (res.code == 0) {
            res.data.data.forEach(res => {
              switch (res.is_account) {
                case 1:
                  res.is_accountMsg = "是";
                  break;
                case 2:
                  res.is_accountMsg = "否";
                  break;
              }
              switch (res.is_can_able) {
                case 1:
                  res.is_can_ableMsg = "使用中";
                  break;
                case 2:
                  res.is_can_ableMsg = "弃用";
                  break;
              }
            });
            this.tableData = res.data.data;
            this.pageData.page = res.data.current_page;
            this.pageData.pagesize = res.data.per_page;
            this.pageData.total = res.data.total;
          }
        })
        .catch(err => {
          console.log(err);
        });
    },

    //删除
    handleDelete(scope) {
      let data = {
        id: scope.row.id
      };
      this.$confirm("是否弃用该条信息?", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(() => {
          cancelItem(data).then(res => {
            if (res.code == 0) {
              this.getData();
              this.$message({
                type: "success",
                message: res.msg || res.message
              });
            }
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消"
          });
        });
    },

    // 本地搜索
    handleSearch() {
      this.pageData.page = 1;
      this.pageData.total = null;
      this.getData();
    },
    //添加
    addAllot() {
      this.dialog.dialogVisible = true;
      this.dialog.dialogType = "";
      this.dialog.formData = {};
      this.dialog.id = "";
    },
    //编辑
    handleEdit(scope) {
      this.dialog.dialogVisible = true;
      this.dialog.dialogType = "edit";
      this.dialog.formData = scope.row;
      this.dialog.id = scope.row.id;
    },

    //每页多少条
    handleCurrentChange(val) {
      this.pageData.page = 1;
      this.pageData.pageSize = val;
      this.getData();
    },
    //选取页数
    handleSizeChange(page) {
      this.pageData.page = page;
      this.getData();
    }
  }
};
</script>
<style scoped>
.top {
  position: relative;
  margin-bottom: 5px;
}
.btnList {
  position: relative;
  margin-bottom: 5px;
}
.add {
  position: absolute;
  right: 0;
  top: 4px;
}
.pagination {
  text-align: right;
  padding: 10px;
}
</style>
